package com.asistencia.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.asistencia.dao.interfaces.TypeCourseDao;
import com.asistencia.domain.TypeCourse;

@Repository
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TypeCourseJdbc implements TypeCourseDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<TypeCourse> listAll() throws DataAccessException {
		String SQL="SELECT * FROM type_course";
		List matches=jdbcTemplate.query(SQL, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				TypeCourse type= new TypeCourse();
				type.setIdTypeCourse(rs.getInt("idtype_course"));
				type.setDescription(rs.getString("description"));
				return type;
						
			}});
		return (matches.size() > 0 ? (List<TypeCourse>) matches : null);
	}	
			
		

	@Override
	public TypeCourse getById(Integer idTypeCourse) throws DataAccessException {
		String SQL ="SELECT * FROM type_course WHERE idtype_course= ? LIMIT 1 ";
		List matches = jdbcTemplate.query(SQL, new Object[]{idTypeCourse}, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				TypeCourse type= new TypeCourse();
				type.setIdTypeCourse(rs.getInt("idtype_course"));
				type.setDescription(rs.getString("description"));
				return type;
			}
		});
			return (matches.size() > 0 ? (TypeCourse) matches.get(0) : null);
		}
			
	}